//
//  YJYPictureBrowsersPopAnimation.m
//  BannerViewDemo
//
//  Created by BaizYi on 2018/11/5.
//  Copyright © 2018 BaizYi. All rights reserved.
//

#import "YJYPictureBrowsersPopAnimation.h"
#import "YJYPictureBrowsersTransitionParamer.h"

@implementation YJYPictureBrowsersPopAnimation

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.7;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    //转场过度的容器view
    UIView *containerView = [transitionContext containerView];
    
    //ToVC
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [containerView addSubview:toViewController.view];
    
    //fromVC
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    CGRect fromFrame = self.transitionParamer.secendVCImgFrame;
    CGRect toFrame = [self.transitionParamer.firstVCImageFrames[self.transitionParamer.transitionImgIndex]CGRectValue];
    
    
    //图片背景白色的空白view
    UIView *imgBgWhiteView = [[UIView alloc]initWithFrame:[self.transitionParamer.firstVCImageFrames[self.transitionParamer.transitionImgIndex] CGRectValue]];
    imgBgWhiteView.backgroundColor = [UIColor whiteColor];
    [containerView addSubview:imgBgWhiteView];
    
    //有渐变的黑色背景
    UIView *bgView = [[UIView alloc]initWithFrame:containerView.bounds];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 1;
    [containerView addSubview:bgView];
    
    //过渡的图片
    UIImageView *transitionImg = [[UIImageView alloc]initWithImage:self.transitionParamer.transitionImage];
//    transitionImg.contentMode = UIViewContentModeScaleAspectFill;
    transitionImg.image = self.transitionParamer.transitionImage;
    transitionImg.frame = fromFrame;
    [transitionContext.containerView addSubview:transitionImg];
    
    
    //动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:0.3 options:UIViewAnimationOptionCurveLinear animations:^{
        
        //避免frame数值为零的处理
        CGRect imageFrame = toFrame;
        if (imageFrame.size.width == 0 && imageFrame.size.height == 0) {
            CGFloat defaultWidth = 5;
            imageFrame = CGRectMake((SCREENWIDTH - defaultWidth) * 0.5, (SCREENHEIGHT - defaultWidth) * 0.5, defaultWidth, defaultWidth);
        }
        transitionImg.frame = imageFrame;
        bgView.alpha = 0;
        
    } completion:^(BOOL finished) {
//        toViewController.view.hidden = NO;
        [imgBgWhiteView removeFromSuperview];
        [bgView removeFromSuperview];
        [transitionImg removeFromSuperview];
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
    
    
//    
//    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
//        CGRect imageFrame = toFrame;
//        if (imageFrame.size.width == 0) {
//            CGFloat defaultWidth = 5;
//            imageFrame = CGRectMake((SCREENWIDTH - defaultWidth) * 0.5, (SCREENHEIGHT - defaultWidth) * 0.5, defaultWidth, defaultWidth);
//        }
//        transitionImg.frame = imageFrame;
//        bgView.alpha = 0;
//    } completion:^(BOOL finished) {
//        toViewController.view.hidden = NO;
//        [imgBgWhiteView removeFromSuperview];
//        [bgView removeFromSuperview];
//        [transitionImg removeFromSuperview];
//        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
//    }];
    
}

@end
